package com.huizu.project.apiController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huizu.common.annotation.Anonymous;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.core.page.TableDataInfo;
import com.huizu.common.enums.BusinessType;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.project.config.TerAspect;
import com.huizu.project.config.UserAspect;
import com.huizu.project.domain.*;
import com.huizu.project.service.IProUserService;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户管理Controller
 * 
 * @author zhangmx
 * @date 2024-05-10
 */
@Anonymous
@RestController
@RequestMapping("/api/prouser")
public class ApiProUserController extends BaseController
{
    @Autowired
    private IProUserService proUserService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询用户管理列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProUser proUser)
    {
//        startPage();
        List<Map<String, Object>> list = proUserService.selectProUserListApi(proUser);
        return getDataTable(list);
    }

    /**
     * 查询学生全部数据列表
     */
    @PostMapping("/listSt")
    public TableDataInfo listSt(@RequestBody ProUser proUser)
    {
        startPage();
        List<ProUser> list = proUserService.listSt(proUser);
        return getDataTable(list);
    }


    /**
     * 获取用户管理详细信息
     */
    @UserAspect
    @PostMapping(value = "/getById")
    public AjaxResult getInfo(HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        return success(proUserService.selectProUserByUserId(userId));
    }


    /**
     * 修改用户管理
     */
    @UserAspect
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody ProUser proUser,HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proUser.setUserId(userId);
        return toAjax(proUserService.updateProUser(proUser));
    }

    @UserAspect
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(@RequestBody ProUser proUser,HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proUser.setUserId(userId);
        return proUserService.updatePassword2(proUser);
    }

    @TerAspect
    @PostMapping("/getBj")
    public AjaxResult getBj(HttpServletRequest request,Map<String,Object> map){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        map.put("userId",userId);
        return proUserService.getBjTree(map);
    }

    @PostMapping("/getXyList")
    public TableDataInfo getXyList(ProXy proXy){
        return getDataTable(proUserService.getXyList(proXy));
    }

    @PostMapping("/getZyList")
    public TableDataInfo getZyList(ProZy proZy){
        return getDataTable(proUserService.getZyList(proZy));
    }

    @PostMapping("/getNjList")
    public TableDataInfo getNjList(ProNj proNj){
        return getDataTable(proUserService.getNjList(proNj));
    }

    @PostMapping("/getBjList")
    public TableDataInfo getBjList(HttpServletRequest request,ProBj proBj){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);
        SysUserAuth sysUserAuthPram = new SysUserAuth();
        sysUserAuthPram.setUserId(Long.valueOf(userId));
        List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
        List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
        proBj.setBjIds(bjs);
        startPage();
        return getDataTable(proUserService.getBjList(proBj));
    }

    @PostMapping("/getBjById")
    public AjaxResult getBjById(@RequestBody ProBj proBj){
        String id = proBj.getId();
        return proUserService.getBjById(id);
    }

    @PostMapping("/getCountCj")
    public AjaxResult getCountCj(Map<String,Object> map,HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        map.put("userId",userId);
        return proUserService.getCountCj(map);
    }

    @PostMapping("/getCountCjByUser")
    public AjaxResult getCountCjByUser(@RequestBody Map<String,Object> map,HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        return proUserService.getCountCj(map);
    }

    @PostMapping("/listPhb")
    public TableDataInfo listPhb(@RequestBody ProUser proUser,HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        ProUser user=  proUserService.selectProUserByUserId(userId);
        proUser.setXyId(user.getXyId());
        return getDataTable(proUserService.listPhb(proUser));
    }

    /**
     * 副本：返回参数base64加密
     * @param proUser
     * @param request
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("/listPhbDeputy")
    public AjaxResult listPhbDeputy(@RequestBody ProUser proUser,HttpServletRequest request) throws JsonProcessingException {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        ProUser user = proUserService.selectProUserByUserId(userId);
        proUser.setXyId(user.getXyId());

        List<ProUser> proUsers = proUserService.listPhb(proUser);
        // 通过base64进行加密
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(proUsers);
        byte[] encodedBytes = Base64.getUrlEncoder().encode(json.getBytes(StandardCharsets.UTF_8));
        // 将字节数组转换为字符串
        String encodedString = new String(encodedBytes);
        return success(encodedString);
    }

    @PostMapping("/listPhbUser")
    public AjaxResult listPhbUser(HttpServletRequest request,@RequestBody Map<String,Object> map){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        map.put("userId",userId);
        return proUserService.listPhbByUser(map);
    }

    @PostMapping("/listLdUser")
    public TableDataInfo listLdUser(HttpServletRequest request,Map<String,Object> map){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        map.put("userId",userId);
        return getDataTable(proUserService.ldListUser(map));
    }

    @PostMapping("/listMessage")
    public AjaxResult listMessage(HttpServletRequest request,Map<String,Object> map){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        map.put("userId",userId);
        return proUserService.listMessage(map);
    }


    @PostMapping("/bindWx")
    public AjaxResult bindWx(HttpServletRequest request,@RequestBody Map<String,Object> map){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        map.put("userId",userId);
        return proUserService.bindWx(map);
    }

    /**
     * 解除绑定
     * @param request
     * @return
     */
    @PostMapping("/relieveBinding")
    public AjaxResult relieveBinding(HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        ProUser proUser = new ProUser();
        proUser.setUserId(userId);
        return proUserService.relieveBinding(proUser);
    }


}
